<?php

namespace App\Services\DingDing;

use AlibabaCloud\SDK\Dingtalk\Vyida_1_0\Models\ListConnectorInformationResponseBody\pluginInfos\apps;
use Illuminate\Support\Facades\DB;
use GuzzleHttp\Client;
class DingDingSerTest
{

    public static $appKey = 'ding2bsidpnaqga1rwfh';
    public static $appsecret='aPwyFZnE20ZdkfBjUP5E8ex10W1XQ7HLCMGv_JEtSIZKHbFz0GPyW4e62kA1UPyG';


    public static function getAccessToken()
    {
        // $url='https://oapi.dingtalk.com/gettoken';

        // $res=file_get_contents($url.'?appkey='.self::$appKey.'&appsecret='.self::$appsecret);

        // //{"errcode":0,"access_token":"4ed176f7f88c3ca288f8ffb1a0e50e61","errmsg":"ok","expires_in":7200}
        // print_r($res);exit;


         $url='https://oapi.dingtalk.com/gettoken';

        $client= new Client();
        $response = $client->request('GET', $url.'?appkey='.self::$appKey.'&appsecret='.self::$appsecret,
        ['headers' => ['Content-Type' => 'application/json;charset=utf-8']]);
        print_r($response->getBody()->getContents());
        //{"errcode":0,"access_token":"638fc41ef68537cdaad14d431e2d1544","errmsg":"ok","expires_in":7200}
    }


    public static function getDepartmentList(){
        $url='https://oapi.dingtalk.com/topapi/v2/department/listsub';

        $client= new Client();
        $response = $client->request('GET', $url.'?access_token=a4593728d7fc33e4afe1c3138663c996',
        ['headers' => ['Content-Type' => 'application/json;charset=utf-8']
        // ,'body' => json_encode(['dept_id'=>'925816929'],JSON_UNESCAPED_UNICODE)
    ]);
        echo $response->getBody()->getContents();
        print_r(json_decode($response->getBody()->getContents(),true));
        //{"errcode":0,"errmsg":"ok","result":[{"auto_add_user":true,"create_dept_group":true,"dept_id":925816929,"ext":"{\"faceCount\":\"81\"}","name":"LH","parent_id":1}],"request_id":"15t24gwfkqb26"}
        /**
         * 
Array
(
    [errcode] => 0
    [errmsg] => ok
    [result] => Array
        (
            [0] => Array
                (
                    [auto_add_user] => 1
                    [create_dept_group] => 1
                    [dept_id] => 925816929
                    [ext] => {"faceCount":"81"}
                    [name] => LH
                    [parent_id] => 1
                )

        )

    [request_id] => 16ki63i06no7e
)
         */
    }



    public static function getSubDepartmentList(){
        $url='https://oapi.dingtalk.com/topapi/v2/department/listsubid';

        $data=['dept_id'=>'925816929'];
        $client= new Client();
        
        $response = $client->request('POST', $url.'?access_token=638fc41ef68537cdaad14d431e2d1544',
        ['headers' => ['Content-Type' => 'application/json;charset=utf-8'],
         'body' => json_encode($data,JSON_UNESCAPED_UNICODE)]);




        print_r(json_decode($response->getBody()->getContents(),true));
        //{"errcode":0,"errmsg":"ok","result":[{"auto_add_user":true,"create_dept_group":true,"dept_id":925816929,"ext":"{\"faceCount\":\"81\"}","name":"LH","parent_id":1}],"request_id":"15t24gwfkqb26"}
        /**
         * 


Array
(
    [errcode] => 0
    [errmsg] => ok
    [result] => Array
        (
            [dept_id_list] => Array
                (
                    [0] => 926121890
                    [1] => 925780892
                    [2] => 928337072
                    [3] => 926009959
                    [4] => 927992806
                    [5] => 926131897
                    [6] => 928138879
                )

        )

    [request_id] => 16krlydyv08ra
)

         */
    }


    //部门详情：https://oapi.dingtalk.com/topapi/v2/department/get
    public static function getDepartmentInfo(){
        $url='https://oapi.dingtalk.com/topapi/v2/department/get';

        $data=['dept_id'=>'926121890'];
        $client= new Client();
        
        $response = $client->request('POST', $url.'?access_token=638fc41ef68537cdaad14d431e2d1544',
        ['headers' => ['Content-Type' => 'application/json;charset=utf-8'],
         'body' => json_encode($data,JSON_UNESCAPED_UNICODE)]);




        print_r(json_decode($response->getBody()->getContents(),true));
        //{"errcode":0,"errmsg":"ok","result":[{"auto_add_user":true,"create_dept_group":true,"dept_id":925816929,"ext":"{\"faceCount\":\"81\"}","name":"LH","parent_id":1}],"request_id":"15t24gwfkqb26"}
        /**
         * 



Array
(
    [errcode] => 0
    [errmsg] => ok
    [result] => Array
        (
            [auto_add_user] => 1
            [auto_approve_apply] => 
            [brief] => 
            [create_dept_group] => 1
            [dept_group_chat_id] => chat0d0f8762839c47dbf7b95baa920dec5d
            [dept_id] => 925816929
            [dept_permits] => Array
                (
                )

            [emp_apply_join_dept] => 
            [extention] => {"faceCount":"81"}
            [group_contain_sub_dept] => 
            [hide_dept] => 1
            [hide_scene_config] => Array
                (
                    [active] => 
                    [chatbox_subtitle] => 1
                    [node_list] => 1
                    [profile] => 1
                    [search] => 1
                )

            [name] => LH
            [order] => 925816929
            [org_dept_owner] => 0245566959021039924
            [outer_dept] => 1
            [outer_permit_depts] => Array
                (
                )

            [outer_permit_users] => Array
                (
                )

            [outer_scene_config] => Array
                (
                    [active] => 
                    [chatbox_subtitle] => 1
                    [node_list] => 1
                    [profile] => 1
                    [search] => 1
                )

            [parent_id] => 1
            [user_permits] => Array
                (
                )

        )

    [request_id] => 15rvrsglf2ger
)


         */
    }

    //获取部门用户userid列表
    public static function getDepartmentUserIdList()
    {


         $url='https://oapi.dingtalk.com/topapi/user/listid';

         $data=[
            'dept_id'=>'926121890'
        ];
         $client= new Client();
         
         $response = $client->request('POST', $url.'?access_token=638fc41ef68537cdaad14d431e2d1544',
         ['headers' => ['Content-Type' => 'application/json;charset=utf-8'],
          'body' => json_encode($data,JSON_UNESCAPED_UNICODE)]);
 
 
 
 
         print_r(json_decode($response->getBody()->getContents(),true));

         /**
          * 

Array
(
    [errcode] => 0
    [errmsg] => ok
    [result] => Array
        (
            [userid_list] => Array
                (
                    [0] => 143950124423478212
                    [1] => 290851434136527455
                    [2] => 140422406124150798
                    [3] => 010957176320253217
                    [4] => 292801466529069230
                    [5] => 232213574835447756
                    [6] => 2908245207679697
                    [7] => 143455663530835544
                    [8] => 1404375608778413
                    [9] => 01275568474624252768
                    [10] => 3163364946685679
                    [11] => 1404255617645990
                    [12] => 1435042423706974
                )

        )

    [request_id] => 16lxpa524tja4
)

          */
    }


    //查询用户详情
    public static function getDepartmentUserInfo()
    {


         $url='https://oapi.dingtalk.com/topapi/v2/user/get';

         $data=[
            'userid'=>'143950124423478212'
        ];
         $client= new Client();
         
         $response = $client->request('POST', $url.'?access_token=638fc41ef68537cdaad14d431e2d1544',
         ['headers' => ['Content-Type' => 'application/json;charset=utf-8'],
          'body' => json_encode($data,JSON_UNESCAPED_UNICODE)]);
 
 
 
 
         print_r(json_decode($response->getBody()->getContents(),true));

         /**
          * 

Array
(
    [errcode] => 0
    [errmsg] => ok
    [result] => Array
        (
            [active] => 1
            [admin] => 
            [avatar] => 
            [boss] => 
            [create_time] => 2024-10-07T03:06:57.000Z
            [dept_id_list] => Array
                (
                    [0] => 926121890
                    [1] => 927364708
                )

            [dept_order_list] => Array
                (
                    [0] => Array
                        (
                            [dept_id] => 926121890
                            [order] => 176189351361673510
                        )

                    [1] => Array
                        (
                            [dept_id] => 927364708
                            [order] => 176189351329189512
                        )

                )

            [exclusive_account] => 
            [hide_mobile] => 
            [leader_in_dept] => Array
                (
                    [0] => Array
                        (
                            [dept_id] => 926121890
                            [leader] => 
                        )

                    [1] => Array
                        (
                            [dept_id] => 927364708
                            [leader] => 
                        )

                )

            [name] => 尹建超
            [real_authed] => 
            [role_list] => Array
                (
                    [0] => Array
                        (
                            [group_name] => HKGL
                            [id] => 4193048646
                            [name] => GL业务
                        )

                )

            [senior] => 
            [unionid] => weiiXuqy3Y4ke6vZ1x55C3AiEiE
            [userid] => 143950124423478212
        )

    [request_id] => 15rw7dpkwuss9
)

          */
    }


    //获取部门用户详情

    public static function getDepartmentUserInfoList()
    {


         $url='https://oapi.dingtalk.com/topapi/v2/user/list';

         $data=[
            'dept_id'=>'926121890',
            'cursor'=>0,
            'size'=>100
        ];
         $client= new Client();
         
         $response = $client->request('POST', $url.'?access_token=638fc41ef68537cdaad14d431e2d1544',
         ['headers' => ['Content-Type' => 'application/json;charset=utf-8'],
          'body' => json_encode($data,JSON_UNESCAPED_UNICODE)]);
 
 
 
 
         print_r(json_decode($response->getBody()->getContents(),true));

         /**
          * 

Array
(
    [errcode] => 0
    [errmsg] => ok
    [result] => Array
        (
            [has_more] => 
            [list] => Array
                (
                    [0] => Array
                        (
                            [active] => 1
                            [admin] => 1
                            [avatar] => 
                            [boss] => 
                            [dept_id_list] => Array
                                (
                                    [0] => 925780892
                                    [1] => 926131897
                                    [2] => 926009959
                                    [3] => 926121890
                                    [4] => 1
                                    [5] => 928007766
                                    [6] => 927992806
                                    [7] => 928337072
                                )

                            [dept_order] => 176203848658241512
                            [exclusive_account] => 
                            [hide_mobile] => 
                            [job_number] => 
                            [leader] => 
                            [name] => 刘温
                            [title] => 
                            [unionid] => Ht9ILwmAwulAp9g3LNBrrgiEiE
                            [userid] => 2908245207679697
                        )

                    [1] => Array
                        (
                            [active] => 1
                            [admin] => 
                            [avatar] => https://static-legacy.dingtalk.com/media/lALPGnqT-wKmYvDNAyDNAyA_800_800.png
                            [boss] => 
                            [dept_id_list] => Array
                                (
                                    [0] => 926121890
                                    [1] => 927390700
                                )

                            [dept_order] => 176189353790238512
                            [exclusive_account] => 
                            [hide_mobile] => 
                            [leader] => 
                            [name] => 赵亚嫱
                            [unionid] => dsgOMv6hQme9yiiTboMlyagiEiE
                            [userid] => 232213574835447756
                        )

                    [2] => Array
                        (
                            [active] => 1
                            [admin] => 
                            [avatar] => https://static-legacy.dingtalk.com/media/lADPBbCc1RNZpW7NAf3NAkA_576_509.jpg
                            [boss] => 
                            [dept_id_list] => Array
                                (
                                    [0] => 926121890
                                    [1] => 927364708
                                )

                            [dept_order] => 176189353790238511
                            [exclusive_account] => 
                            [hide_mobile] => 
                            [leader] => 
                            [name] => 徐东刚
                            [unionid] => 8o9gb26wLjqK0vfh2zxsnAiEiE
                            [userid] => 140422406124150798
                        )

                    [3] => Array
                        (
                            [active] => 1
                            [admin] => 
                            [avatar] => 
                            [boss] => 
                            [dept_id_list] => Array
                                (
                                    [0] => 926121890
                                    [1] => 927364708
                                )

                            [dept_order] => 176189353737063512
                            [exclusive_account] => 
                            [hide_mobile] => 
                            [leader] => 
                            [name] => 秦文龙
                            [unionid] => iP4lAnf1eFEh9wUTQoiSrCzgiEiE
                            [userid] => 143455663530835544
                        )

                    [4] => Array
                        (
                            [active] => 1
                            [admin] => 
                            [avatar] => 
                            [boss] => 
                            [dept_id_list] => Array
                                (
                                    [0] => 927364708
                                    [1] => 928354063
                                    [2] => 927390700
                                    [3] => 926121890
                                )

                            [dept_order] => 176189353190770512
                            [exclusive_account] => 
                            [hide_mobile] => 
                            [leader] => 
                            [name] => 徐丽
                            [unionid] => kiS2j9ggzHSii9yiiTboMlyagiEiE
                            [userid] => 1404375608778413
                        )

                    [5] => Array
                        (
                            [active] => 1
                            [admin] => 
                            [avatar] => 
                            [boss] => 
                            [dept_id_list] => Array
                                (
                                    [0] => 926121890
                                    [1] => 927337969
                                    [2] => 928337072
                                )

                            [dept_order] => 176189352896589510
                            [exclusive_account] => 
                            [hide_mobile] => 
                            [leader] => 
                            [name] => 刘薇
                            [unionid] => IphLFWgTsZGGDBuxQpKii6giEiE
                            [userid] => 3163364946685679
                        )

                    [6] => Array
                        (
                            [active] => 1
                            [admin] => 
                            [avatar] => 
                            [boss] => 
                            [dept_id_list] => Array
                                (
                                    [0] => 926121890
                                    [1] => 927337969
                                    [2] => 928337072
                                )

                            [dept_order] => 176189352896589509
                            [exclusive_account] => 
                            [hide_mobile] => 
                            [leader] => 
                            [name] => 王亚丽
                            [unionid] => QTZkStgbKap9wUTQoiSrCzgiEiE
                            [userid] => 292801466529069230
                        )

                    [7] => Array
                        (
                            [active] => 1
                            [admin] => 
                            [avatar] => 
                            [boss] => 
                            [dept_id_list] => Array
                                (
                                    [0] => 925780892
                                    [1] => 927337969
                                    [2] => 928337072
                                    [3] => 926121890
                                    [4] => 927913013
                                )

                            [dept_order] => 176189351833239512
                            [exclusive_account] => 
                            [hide_mobile] => 
                            [leader] => 
                            [name] => 张晓阳
                            [unionid] => 0Wmr6MVsqzDGiSjhOiS1c6vgiEiE
                            [userid] => 01275568474624252768
                        )

                    [8] => Array
                        (
                            [active] => 1
                            [admin] => 
                            [avatar] => 
                            [boss] => 
                            [dept_id_list] => Array
                                (
                                    [0] => 927364708
                                    [1] => 926121890
                                )

                            [dept_order] => 176189351815203512
                            [exclusive_account] => 
                            [hide_mobile] => 
                            [leader] => 
                            [name] => 侯亚斌
                            [unionid] => qqntMjSw0RgrqN7ZRlrYgwiEiE
                            [userid] => 010957176320253217
                        )

                    [9] => Array
                        (
                            [active] => 1
                            [admin] => 
                            [avatar] => 
                            [boss] => 
                            [dept_id_list] => Array
                                (
                                    [0] => 926121890
                                    [1] => 927364708
                                )

                            [dept_order] => 176189351361673510
                            [exclusive_account] => 
                            [hide_mobile] => 
                            [leader] => 
                            [name] => 尹建超
                            [unionid] => weiiXuqy3Y4ke6vZ1x55C3AiEiE
                            [userid] => 143950124423478212
                        )

                    [10] => Array
                        (
                            [active] => 1
                            [admin] => 
                            [avatar] => 
                            [boss] => 
                            [dept_id_list] => Array
                                (
                                    [0] => 926121890
                                    [1] => 927364708
                                )

                            [dept_order] => 176189351361673508
                            [exclusive_account] => 
                            [hide_mobile] => 
                            [leader] => 
                            [name] => 乔强
                            [unionid] => rF8h4wmbvWxAp9g3LNBrrgiEiE
                            [userid] => 1404255617645990
                        )

                    [11] => Array
                        (
                            [active] => 1
                            [admin] => 
                            [avatar] => 
                            [boss] => 
                            [dept_id_list] => Array
                                (
                                    [0] => 926121890
                                    [1] => 927364708
                                )

                            [dept_order] => 176189350700475510
                            [exclusive_account] => 
                            [hide_mobile] => 
                            [leader] => 
                            [name] => 吴青
                            [unionid] => WzmYiP309JxGiiGZQiS56C4OgiEiE
                            [userid] => 1435042423706974
                        )

                    [12] => Array
                        (
                            [active] => 1
                            [admin] => 
                            [avatar] => https://static-legacy.dingtalk.com/media/lADPDgQ9q_xr5EXNASzNASw_300_300.jpg
                            [boss] => 
                            [dept_id_list] => Array
                                (
                                    [0] => 926009959
                                    [1] => 928337072
                                    [2] => 925780892
                                    [3] => 926131897
                                    [4] => 926121890
                                )

                            [dept_order] => 176189349480651512
                            [exclusive_account] => 
                            [hide_mobile] => 
                            [leader] => 
                            [name] => 邱焕荣
                            [unionid] => qpKhdIDUqUOK0vfh2zxsnAiEiE
                            [userid] => 290851434136527455
                        )

                )

        )

    [request_id] => 16kww5fg96kve
)

          */
    }
}